package com.zelma.mywebsocket.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zelma.mywebsocket.pojo.ResultMessage;
import com.zelma.mywebsocket.pojo.ResultMessageTwo;
import org.springframework.stereotype.Component;


//封装发送的消息内容
@Component
public class MessageUtils {
    public static String getMessage(boolean isSystemMessage,String fromName,Object message,String status){
        try{
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setSystem(isSystemMessage);
            resultMessage.setMessage(message);
            resultMessage.setStatus(status);
            if(fromName != null){
                resultMessage.setFromName(fromName);
            }
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(resultMessage);
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }
    public static String getMessages(boolean isSystemMessage,String fromName,Object messages,String status){
        try{
            ResultMessageTwo resultMessageTwo = new ResultMessageTwo();
            resultMessageTwo.setSystem(isSystemMessage);
            resultMessageTwo.setMessages(messages);
            resultMessageTwo.setStatus(status);
            if(fromName != null){
                resultMessageTwo.setFromName(fromName);
            }
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(resultMessageTwo);
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }
}
